﻿/*
 * 由SharpDevelop创建。
 * 用户： lucifer
 * 日期: 2011/3/18
 * 时间: 11:24
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using System.Collections.Generic;
using System.Text;

using Yidu.Persister.Mapping;

namespace Yidu.Persister.Generator
{
	/// <summary>
	/// Description of SqlServerSqlGenerator.
	/// </summary>
	public class SqlServerSqlGenerator : ISqlGenerator
	{

		
		public string Save(object entry)
		{
			TableMappingInfo tableMapping =  TableMappingHelper.Instance.GetEntryMapping(entry.GetType());
			if(tableMapping == null){
				return null;
			}
			StringBuilder builder = new StringBuilder();
			StringBuilder valueBuilder = new StringBuilder();
			
			builder.Append("insert into ").Append(tableMapping.TableName).AppendLine();
			builder.Append("(").AppendLine();
			
			valueBuilder.Append("(").AppendLine();
			
			bool first = true;
			
			foreach(KeyValuePair<string,FieldMappingInfo> keyValue in tableMapping.FieldMapping){
				if(first){
					first = false;
				}else{
					builder.Append(",").AppendLine();
					valueBuilder.Append(",").AppendLine();
				}
				builder.Append(keyValue.Value.FieldName);
				valueBuilder.Append("@").Append(keyValue.Value.PropertyName);
			}
			builder.AppendLine().Append(")");
			valueBuilder.AppendLine().Append(")");
			
			builder.AppendLine().Append("values").AppendLine().Append(valueBuilder.ToString());
			
			return builder.ToString();
		}
	}
}
